package com.xuxin.baby.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.ChildGreatAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.ChildGreatBean;
import com.xuxin.baby.contract.ChildGreatContract;
import com.xuxin.baby.presenter.ChildGreatPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.09.09 0009
 * 宝宝大事记
 */
public class ChildGreatActivity extends BaseActivity implements ChildGreatContract.View {
    private final ChildGreatContract.Presenter presenter = new ChildGreatPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private ChildGreatAdapter myAdapter;
    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝ID
    private int pageNum = 0;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        title_name.setText("宝宝大事记");
        smart_refresh.setBackgroundColor(Color.WHITE);
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onRefresh");
                pageNum = 0;
                myAdapter.setList(null);
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onLoadMore");
                handler.sendEmptyMessage(0);
            }

        });
        myAdapter = new ChildGreatAdapter();
        RecycleUtils.init(smart_recycle);
        smart_recycle.setAdapter(myAdapter);
        View header = View.inflate(context, R.layout.item_child_header, null);
        header.findViewById(R.id.item_child_record).setOnClickListener(this);
        header.findViewById(R.id.item_child_statistics).setOnClickListener(this);
        myAdapter.addHeaderView(header);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("detail", true);
                bundle.putString("dynamicContent", myAdapter.getData().get(position).getEventContent());
                bundle.putString("dynamicImage", myAdapter.getData().get(position).getPic1());
                ActivityUtils.startActivity(bundle, ChildGreatRecordActivity.class);
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
        smart_refresh.autoRefresh();
    }

    @Override
    public void onComplete() {
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.item_child_record) {
            ActivityUtils.startActivityForResult(this, ChildGreatRecordActivity.class, 200);
        } else if (id == R.id.item_child_statistics) {
            ToastUtils.showShort("功能尚在完善...");
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getChildGreat(tenantId, token, childId, "basic", pageNum);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(ChildGreatBean childGreatBean) {
        myAdapter.addData(childGreatBean.getResult().getItems());
        if (childGreatBean.getResult().getTotalCount() > myAdapter.getData().size()) {
            pageNum++;
            smart_refresh.setEnableLoadMore(true);
        } else {
            smart_refresh.setEnableLoadMore(false);
        }
    }
}